package com.rock.goc.data.service.impl;

import com.rock.common.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rock.common.utils.PageUtils;
import com.rock.common.utils.Query;

import com.rock.goc.data.dao.WorkshopDao;
import com.rock.goc.data.entity.WorkshopEntity;
import com.rock.goc.data.service.WorkshopService;


@Service("workshopService")
public class WorkshopServiceImpl extends ServiceImpl<WorkshopDao, WorkshopEntity> implements WorkshopService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WorkshopEntity> page = this.page(
                new Query<WorkshopEntity>().getPage(params),
                new QueryWrapper<WorkshopEntity>()
        );
        List<WorkshopEntity> records = page.getRecords();
        records.stream().map(Bean->Bean.getFactoryId()).collect(Collectors.toList());
        return new PageUtils(page);
    }

    @Override
    public R saveWorkshop(WorkshopEntity workshop) {
        List<WorkshopEntity> list = list(new QueryWrapper<WorkshopEntity>()
                .eq("factory_id", workshop.getFactoryId())
                .eq("name", workshop.getName()));
        if (list.size() > 0){
            return R.error().put("msg", "这个工厂已经有这个车间了, 不要新增了");
        }else{
            save(workshop);
            return R.ok();
        }
    }

}